/*
 * Usuario.java
 *
 * Created on 7 de Maio de 2007, 06:29
 *
 */

package net.sf.jOpinionPoll.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Transient;
import javax.persistence.Version;

/**
 * Entity class Usuario
 *
 * @author erico
 */
@Entity
@Table(name = "questionario")
@NamedQueries({
		@NamedQuery(name = "Questionario.fetch.findById", query = "SELECT q FROM Questionario q WHERE LOWER(q.id) = :id"),
        @NamedQuery(name = "Questionario.findAll", query = "SELECT q FROM Questionario q"),
        @NamedQuery(name = "Questionario.count", query = "SELECT COUNT(q) FROM Questionario q")
         })
public class Questionario implements Serializable {
	private static final long serialVersionUID = 1L;
	
    private int id;
    private String nome;
    private Date data;
    private String descricao;
    private Set<Questao> questao = new HashSet<Questao>();
    private int version;

    public Questionario() {
    }
    
    public Questionario(String nome) {
        this.nome = nome;
    }
  
    public Questionario(String nome, Date data, String descricao) {
        setNome(nome);
        setData(data);
        setDescricao(descricao);
    }

    @Id
    @Column(name = "id")
    @TableGenerator(
            name="questionarioGen",
            table="sequence_generator_table",
            pkColumnName="gen_key",
            pkColumnValue="questionario_id",
            valueColumnName="gen_value",
            allocationSize=50,
            initialValue=0)
    @GeneratedValue(
            strategy=GenerationType.TABLE,
            generator="questionarioGen")    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    @Column(name = "nome", nullable = false)
    public String getNome() {
        return nome;
    }
    
    public void setNome(String nome) {
        this.nome = (nome != null ? nome.trim() : null);
    }
    
    @Column(name = "data", nullable = false)
    public Date getData() {
        return data;
    }
    
    public void setData(Date data) {
        this.data = data;
    }
    
    @Column(name = "descricao", nullable = true)
    public String getDescricao() {
        return this.descricao;
    }
    
    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }
    
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "questionario_questao",
    		joinColumns=@JoinColumn(name = "questionario_id", referencedColumnName = "id"),
    		inverseJoinColumns=@JoinColumn(name = "questao_id", referencedColumnName = "id")
    )
    public Set<Questao> getQuestoes() {
        return this.questao;
    }
    
    public void setQuestoes(Set<Questao> questoes) {
    	this.questao = questoes;
    }
    
    @Version
    public int getVersion() {
    	return this.version;
    }
    
    public void setVersion(int version) {
    	this.version = version;
    }
    
    @Transient
    public int countQuestions() {
    	return getQuestoes().size();
    }

    /**
     * Returns a hash code value for the object.  This implementation computes
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        return getId();
    }
    
    /**
     * Determines whether another object is equal to this Questionario.  The result is
     * <code>true</code> if and only if the argument is not null and is a Questionario object that
     * has the same id field values as this object.
     * @param o the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object o) {
    	if (o == null || !(o instanceof Questionario))
            return false;

        if (getId() != 0)
	        return getId() == ((Questionario)o).getId();
        
        return this == o;
    }
    
    /**
     * Returns a string representation of the object.  This implementation constructs
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return this.getClass().getName() + "[pk=" + getNome() + "]";
    }
}